// src/http/decorators/mock.ts
import { BusinessError } from "@kit.BasicServicesKit";
import { http } from "@kit.NetworkKit";
import { ApiResult } from "../model/ApiResult";
import { HttpRequestConfig, HttpResponse } from "../http/types";

// 声明 AppStorage 对象
declare const AppStorage: {
    get(key: string): boolean | null;
};

// 自定义 MyMock 装饰器
export function MyMock(targetObj: object, methodName: string, methodDescriptor: PropertyDescriptor) {
    // 保存原始方法
    const originalMethod = methodDescriptor.value;

    // 重新定义方法
    methodDescriptor.value = function (...params: any[]) {
        // 判断是否处于 mock 模式
        const isMockMode = AppStorage.get('isMock') === true;
        if (isMockMode) {
            // 执行模拟数据生成逻辑
            const mockResult = Reflect.apply(mockFunctions[methodName], null, params);
            console.log(`-----result: ${JSON.stringify(mockResult)}`);
            return mockResult;
        } else {
            console.log("函数开始执行...");
            // 调用原始方法并确保 this 指向正确
            const realResult = originalMethod.apply(this, params);
            console.log("函数执行结束...");
            console.log(`result= ${JSON.stringify(realResult)}`);
            return realResult;
        }
    };
}

// 模拟登录逻辑
function simulateLogin(userName: string, userPassword: string): Promise<boolean> {
    return new Promise((resolve) => {
        if (userName === "123" && userPassword === "123") {
            resolve(true);
        } else {
            resolve(false);
        }
    });
}

// 存储模拟函数的对象
const mockFunctions = {
    login: simulateLogin
};